package jdk;

import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @description:
 * @author: TanJingyu
 * @create: 2021-12-28 09:40
 **/
public class Aqs {
    private static final ReentrantLock LOCK = new ReentrantLock();
    private static final Condition CONDITION = LOCK.newCondition();
    private static final Executor EXECUTOR = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    private static final ReentrantReadWriteLock REENTRANT_READ_WRITE_LOCK = new ReentrantReadWriteLock();

    public static void main(String[] args) throws InterruptedException {
//        lockOne();

//        lockTwo();

//        readLock();
//        writeLock();

//        countDownLatch();

        executor();
    }

    public static void executor() {
        EXECUTOR.execute(() -> {
            System.out.println("哈哈");
        });
    }

    public static void cyclicBarrier() {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
//        cyclicBarrier.await()
    }

    public static void semaphore() {
        Semaphore semaphore = new Semaphore(2);
    }

    /**
     * 这个工具玩了个反方向思维。
     * 比如说lock，都是获取state，获取不到阻塞。这东西是不需要state，state大于0的时候阻塞。
     * 这东西可以通过调用countDown()和await()代替cyclicBarrier。
     * 这东西我认为更灵活，它的阻塞不仅可以靠参与者共同解除，还能靠外人解除。而cyclicBarrier只能靠参与者共同解除
     */
    private static void countDownLatch() {
        new Thread(() -> {
            CountDownLatch countDownLatch = new CountDownLatch(1);
            countDownLatch.countDown();
//            try {
//                countDownLatch.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }).start();
    }

    private static void writeLock() {
        new Thread(() -> {
            ReentrantReadWriteLock.WriteLock writeLock = REENTRANT_READ_WRITE_LOCK.writeLock();
            writeLock.lock();
            System.out.println("获取到了");
            writeLock.unlock();
        }).start();
    }

    private static void readLock() {
        new Thread(() -> {
            ReentrantReadWriteLock.ReadLock readLock = REENTRANT_READ_WRITE_LOCK.readLock();
            readLock.lock();
            System.out.println("获取到了锁头");
            readLock.unlock();
        }).start();
    }

    private static void lockTry() {
        new Thread(() -> {
//            LOCK.lock();

//            try {
//                LOCK.lockInterruptibly();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }

//            LOCK.tryLock();

//            try {
//                LOCK.tryLock(20L, TimeUnit.DAYS);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }

//            try {
//                CONDITION.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }).start();
    }

    private static void lockTwo() throws InterruptedException {
        TimeUnit.SECONDS.sleep(3);
        new Thread(() -> {
            LOCK.lock();
            CONDITION.signalAll();
            LOCK.unlock();
        }).start();
    }

    public static void lockOne() throws InterruptedException {
        new Thread(() -> {
            LOCK.lock();

            try {
//                TimeUnit.SECONDS.sleep(5);
                CONDITION.await();
                System.out.println("重新锁头");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

//            LOCK.unlock();
        }).start();
    }


}

    